package com.heima.admin.service.impl;

import com.alibaba.nacos.common.util.Md5Utils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdUserMapper;
import com.heima.admin.service.AdUserService;
import com.heima.common.constants.admin.AdminConstants;
import com.heima.model.admin.dtos.AdUserDto;
import com.heima.model.admin.pojos.AdUser;
import com.heima.model.admin.vo.AdUserVo;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.AppJwtUtil;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 *
 */
@Service
public class AdUserServiceImpl extends ServiceImpl<AdUserMapper,AdUser> implements AdUserService {
    @Override
    public ResponseResult login(AdUserDto dto) {
        // 检查参数
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        // 判断是否有这个用户
        String name = dto.getName();
        // 基于用户名查询用户信息
        LambdaQueryWrapper<AdUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdUser::getName,name);
        AdUser one = getOne(wrapper);

        if (one == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.USER_NOT_EXIST);
        }
        // 判断状态
        if (one.getStatus()!= AdminConstants.AD_USER_ISSTATUS){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_USER_STATUS);
        }

        // 如果用户存在，检查密码是否正确
        // 输入的密码需要进行加密后比较
        String password = Md5Utils.getMD5((dto.getPassword()+one.getSalt()).getBytes());
        String userPsw = one.getPassword();
        if (!password.equals(userPsw)){
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }


        // 更改最后登陆时间
        one.setLoginTime(new Date());
        updateById(one);

        // 颁发凭证
        String token = AppJwtUtil.getToken(Long.valueOf(one.getId()));
        Map<String,Object> map = new HashMap<>();
        AdUserVo adUserVo = new AdUserVo();
        BeanUtils.copyProperties(one,adUserVo);
        map.put("token",token);
        map.put("user",adUserVo);
//        responseResult.setData(map);

        return ResponseResult.okResult(map);
    }

    @Override
    public ResponseResult register(AdUser adUser) {
        if (adUser == null || StringUtils.isBlank(adUser.getName()) || StringUtils.isBlank(adUser.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        // 判断是否重复用户名
        LambdaQueryWrapper<AdUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdUser::getName,adUser.getName());
        AdUser one = getOne(wrapper);
        if (one != null){
            return ResponseResult.errorResult(AppHttpCodeEnum.USER_EXIST);
        }

        // 加盐
        String salt = RandomStringUtils.randomAlphanumeric(8);
        adUser.setSalt(salt);
        adUser.setPassword(DigestUtils.md5DigestAsHex((adUser.getPassword()+salt).getBytes()));

        // 加状态
        adUser.setStatus(AdminConstants.AD_USER_ISSTATUS);
        adUser.setCreatedTime(new Date());

        save(adUser);

        return ResponseResult.okResult();
    }
}
